package com.wst.system.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wst.system.config.cache.SysCacheServer;
import com.wst.system.security.entity.SysUserTokenEntity;
import com.wst.system.server.mapper.SysUserTokenMapper;
import com.wst.system.server.service.SysUserTokenService;
import com.wst.system.util.RandomNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class SysUserTokenServiceImpl extends ServiceImpl<SysUserTokenMapper, SysUserTokenEntity>
    implements SysUserTokenService {

    @Autowired
    private SysCacheServer sysCacheServer;

    @Override
    public SysUserTokenEntity getSysUserTokenByToken(String token) {
        LambdaQueryWrapper<SysUserTokenEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserTokenEntity::getToken,token);
        SysUserTokenEntity mUserToken=this.baseMapper.selectOne(lambdaQueryWrapper);
        return mUserToken;
    }

    @Override
    public SysUserTokenEntity getSysUserTokenByToken(String token, String type) {
        LambdaQueryWrapper<SysUserTokenEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserTokenEntity::getToken,token);
        lambdaQueryWrapper.eq(SysUserTokenEntity::getTokenType,type);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }


    @Override
    public SysUserTokenEntity removTokenByToken(String token) {
        LambdaQueryWrapper<SysUserTokenEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserTokenEntity::getToken,token);
        this.baseMapper.delete(lambdaQueryWrapper);
        return null;
    }

    @Override
    public List<SysUserTokenEntity> getSysUserTokenByUserId(Integer userId, String type, Date date) {
        LambdaQueryWrapper<SysUserTokenEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserTokenEntity::getUserid,userId);
        lambdaQueryWrapper.eq(SysUserTokenEntity::getTokenType,type);
        lambdaQueryWrapper.eq(SysUserTokenEntity::getLogindate,date);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public String getSessionId() {
        //生成请求id,防止重复请求
        String sessionId= RandomNumber.generateRandomNumber(8)+
                String.valueOf(RandomNumber.generateRandomNumber(8))+
                RandomNumber.generateRandomNumber(8) + String.valueOf(RandomNumber.generateRandomNumber(8));
        boolean isSessionId=true;
        while (isSessionId){
            if(sysCacheServer.get(sessionId)==null){
                isSessionId=false;
            }else {
                sessionId= RandomNumber.generateRandomNumber(8)+
                        String.valueOf(RandomNumber.generateRandomNumber(8))+
                        RandomNumber.generateRandomNumber(8) + String.valueOf(RandomNumber.generateRandomNumber(8));
            }
        }
        return sessionId;
    }
}




